package com.ruoyi.common.utils;

import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.data.domain.Car;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 车型解析工具类
 */
public class BasicCarUtils {

    /**
     * 解析车辆信息
     * @param model  型号
     * @return
     */
    public static Car getBasicCar(String model){
        try {
            String url = "https://www.cn357.com/cvi.php";
            String s = HttpUtils.sendGet(url, "m=cvNotice&search=n&model="+model);
            Document document = Jsoup.parse(s);
            Element element = document.getElementsByClass("gMain").first().getElementsByTag("ul").first();
            if (element != null){
                String attr = element.getElementsByTag("a").first().attr("href");
                return getCar(attr);
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(getBasicCar("BJ4259SNFKBAA"));
    }


    private static String REGEX_CHINESE = "[\u4e00-\u9fa5]";// 中文正则

    private static String REGEX_NUM = "[0-9]*";// 数字正则

    public static String getRealModel(String str) {
        // 去除中文
        Pattern pat = Pattern.compile(REGEX_CHINESE);
        Matcher mat = pat.matcher(str);
       return mat.replaceAll("");
    }

    public static Car getCar(String attr){
        try{
            String s1 = HttpUtils.sendGet("https://www.cn357.com" + attr, "");
            Document document2 = Jsoup.parse(s1);
            Element table = document2.getElementsByClass("listTable").select("table").first();
            Elements trs = table.select("tr");
            Car car = new Car();
            int size = trs.size();
            String all_model = trs.get(1).select("td").get(1).text();
            if ("</a>".contains(all_model)) {
                all_model = trs.get(1).select("td").get(1).getElementsByTag("a").text();
            }
            car.setAllModel(all_model);
            car.setModel(all_model.split(" ")[0]);
            car.setBatch(trs.get(1).select("td").get(3).text());
            String brand = trs.get(2).select("td").get(1).text();
            if ("</a>".contains(brand)) {
                brand = trs.get(2).select("td").get(1).getElementsByTag("a").text();
            }
            car.setBrand(brand);
            car.setType(trs.get(2).select("td").get(3).text());
            car.setRatedMass(trs.get(3).select("td").get(1).text());
            car.setAllMass(trs.get(3).select("td").get(3).text());
            car.setCurbWeight(trs.get(4).select("td").get(1).text());
            car.setFuelType(trs.get(4).select("td").get(3).text());
            car.setEmissionStandard(trs.get(5).select("td").get(1).text());
            car.setAxleNumber(trs.get(5).select("td").get(3).text());
            car.setWheelbase(trs.get(6).select("td").get(1).text());
            car.setAxleLoad(trs.get(6).select("td").get(3).text());
            car.setSpringNumber(trs.get(7).select("td").get(1).text());
            car.setTyreNumber(trs.get(7).select("td").get(3).text());
            car.setTireSize(trs.get(8).select("td").get(1).text());
            car.setApproachDepartureAngle(trs.get(8).select("td").get(3).text());
            car.setFrontSuspensionAndRearSuspension(trs.get(9).select("td").get(1).text());
            car.setFrontTrack(trs.get(9).select("td").get(3).text());
            car.setRearTrack(trs.get(10).select("td").get(1).text());
            car.setIdentificationCode(trs.get(10).select("td").get(3).text());
            car.setVehicleLength(trs.get(11).select("td").get(1).text());
            car.setVehicleWidth(trs.get(11).select("td").get(3).text());
            car.setVehicleHeight(trs.get(12).select("td").get(1).text());
            car.setCargoCompartmentLength(trs.get(12).select("td").get(3).text());
            car.setCargoCompartmentWidth(trs.get(13).select("td").get(1).text());
            car.setCargoCompartmentHeight(trs.get(13).select("td").get(3).text());
            car.setMaximumSpeed(trs.get(14).select("td").get(1).text());
            car.setRatedPassengerCapacity(trs.get(14).select("td").get(3).text());
            car.setCarNumber(trs.get(15).select("td").get(1).text());
            car.setSteeringForm(trs.get(15).select("td").get(3).text());
            car.setTotalMassOfQuasiTrailer(trs.get(16).select("td").get(1).text());
            car.setUtilizationCoefficientOfLoadMass(trs.get(16).select("td").get(3).text());
            car.setMaximumBearingMassOfSemiTrailerSaddle(trs.get(17).select("td").get(1).text());
            car.setChassisOne(trs.get(20).select("td").get(1).text());
            car.setChassisTwo(trs.get(20).select("td").get(3).text());
            car.setChassisThree(trs.get(21).select("td").get(1).text());
            car.setChassisFour(trs.get(21).select("td").get(3).text());
            if (size == 23) {
                car.setRemark(trs.get(22).select("td").get(1).text());
            } else if (size == 26) {
                car.setRemark(trs.get(25).select("td").get(1).text());
            }
            return car;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取最新批次
     * @return
     */
    public static String getBatch(){
        String batchHtml = HttpUtils.sendGet("https://www.cn357.com/notice_list", "");
        Document document = Jsoup.parse(batchHtml);
        String attr = document.getElementsByClass("uiLinkList").first().getElementsByTag("a").first().attr("href");
        return attr.split("_")[1];
    }

    /**
     * 获取总页码
     * @param batch
     */
    public static Integer getPageNum(String batch){
        String batchHtml = HttpUtils.sendGet("https://www.cn357.com/notice_"+batch, "");
        Document document = Jsoup.parse(batchHtml);
        // 判断是否有页码
        Element firstPage = document.getElementsByClass("mNewsPage").first();
        Integer pageNum = 1;
        if (firstPage != null){
            List<String> strings = firstPage.getElementsByClass("pageList").first().getElementsByTag("a").eachAttr("href");
            System.out.println(strings);
            if (strings != null && strings.size() >0){
                pageNum = Integer.valueOf(strings.get(strings.size()-2).split("_")[2]);
            }
        }
        return pageNum;
    }

    /**
     * 判断字符串是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile(REGEX_NUM);
        return pattern.matcher(str).matches();
    }

//    @SuppressWarnings("unused")
    public static int chineseNumber2Int(String chineseNumber){
        int result = 0;
        int temp = 1;//存放一个单位的数字如：十万
        int count = 0;//判断是否有chArr
        char[] cnArr = new char[]{'一','二','三','四','五','六','七','八','九'};
        char[] chArr = new char[]{'十','百','千','万','亿'};
        for (int i = 0; i < chineseNumber.length(); i++) {
            boolean b = true;//判断是否是chArr
            char c = chineseNumber.charAt(i);
            for (int j = 0; j < cnArr.length; j++) {//非单位，即数字
                if (c == cnArr[j]) {
                    if(0 != count){//添加下一个单位之前，先把上一个单位值添加到结果中
                        result += temp;
                        temp = 1;
                        count = 0;
                    }
                    // 下标+1，就是对应的值
                    temp = j + 1;
                    b = false;
                    break;
                }
            }
            if(b){//单位{'十','百','千','万','亿'}
                for (int j = 0; j < chArr.length; j++) {
                    if (c == chArr[j]) {
                        switch (j) {
                            case 0:
                                temp *= 10;
                                break;
                            case 1:
                                temp *= 100;
                                break;
                            case 2:
                                temp *= 1000;
                                break;
                            case 3:
                                temp *= 10000;
                                break;
                            case 4:
                                temp *= 100000000;
                                break;
                            default:
                                break;
                        }
                        count++;
                    }
                }
            }
            if (i == chineseNumber.length() - 1) {//遍历到最后一个字符
                result += temp;
            }
        }
        return result;
    }


}
